package com.shj.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shj.mapper.SysMenuDao;
import com.shj.model.PageRequest;
import com.shj.model.PageResult;
import com.shj.model.SysMenu;
import com.shj.util.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description 权限service
 * @Author songhuajian
 * @Date 2020/1/12 23:00
 **/
@Slf4j
@Service("sysPowerService")
public class SysPowerService {

    @Resource
    private SysMenuDao sysMenuDao;

    /**
     * 查找全部
     */
    public List<SysMenu> findAll() {
        return sysMenuDao.queryAll(null);
    }

    /**
     * @description 根据powerId查找
     * @author songhuajian
     * @date 2020/3/24 13:36
     */
    public SysMenu queryByPowerId(String powerId){ return this.sysMenuDao.queryById(powerId); }

    /**
     * @description 分页查询数据
     * @author songhuajian
     * @date 2020/3/24 13:44
     */
    public PageResult findPage(PageRequest pageRequest){
        return PageUtil.getPageResult(pageRequest, getPageInfo(pageRequest));
    }

    /**
     * @description 调用分页插件完成分页
     * @author songhuajian
     * @date 2020/3/24 13:43
     */
    public PageInfo<SysMenu> getPageInfo(PageRequest pageRequest){
        int pageNume = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNume, pageSize);

        List<SysMenu> list = sysMenuDao.queryAll(pageRequest.getTitle());
        return new PageInfo<SysMenu>(list);
    }

    /**
     * @description 新增权限数据
     * @author songhuajian
     * @date 2020/3/25 15:09
     */
    public void insert(SysMenu sysMenu) throws Exception {
        if (sysMenuDao.insert(sysMenu) == 0){
            log.error("添加权限失败");
            throw new Exception("添加权限失败");
        }
    }

    /**
     * @description 更新权限数据
     * @author songhuajian
     * @date 2020/3/25 15:10
     */
    public void update(SysMenu sysMenu) throws Exception {
        if (sysMenuDao.update(sysMenu) == 0){
            log.error("更新权限失败");
            throw new Exception("更新权限失败");
        }
    }

    /**
     * @description 删除权限数据
     * @author songhuajian
     * @date 2020/3/25 15:11
     */
    public void delete(String powerId) throws Exception {
        if (sysMenuDao.deleteById(powerId) == 0){
            log.error("删除权限失败");
            throw new Exception("删除权限失败");
        }
    }

    /**
     * @description 根据路径查询权限
     * @author songhuajian
     * @date 2020/1/12 23:14
     */
    public SysMenu findByUrl(String url){
        return sysMenuDao.queryByUrl(url);
    }
}
